package com.py.credit.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.BatchFeedbackEntity;
import com.py.credit.entity.BatchLog;
import com.py.credit.entity.DictEntity;
import com.py.credit.entity.StepLog;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.Table;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.BatchDetailFieldsEnum;
import com.py.credit.entity.enums.ResourceBatchLogFieldsEnum;
import com.py.credit.service.BatchFeedbackService;
import com.py.credit.service.BatchLogService;
import com.py.credit.service.ErrorDataService;
import com.py.credit.service.ManualService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.service.StepLogService;
import com.py.credit.service.TableService;
import com.py.credit.service.impl.UntreatedFileShedule;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.entity.SysUserEntity;
import com.py.framework.core.helper.HttpServletHelper;

/**
 * 各区县自助上报数据
 * 
 * 
 */
@RestController
@RequestMapping(value = "/manual")
public class ManualController extends BaseController {

	private final static String FRONT_TABLE_ID = "batch";

	@Autowired
	private ManualService manualService;

	@Autowired
	private StepLogService stepLogService;

	@Autowired
	private TableService tableService;

	@Value("${absolutepath}")
	private String absolutepath;

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Autowired
	private UntreatedFileShedule untreatedFileShedule;

	@Autowired
	private BatchFeedbackService batchFeedbackService;
	
	@Autowired
	private ErrorDataService errorDataService;
	

	/**
	 * 获取初始化列
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {
		return new RestServiceResult<List<OptionalColumn>>(ResourceBatchLogFieldsEnum.getOptionalColumns());
	}

	@RequestMapping(value = "/getAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {
		return new RestServiceResult<List<OptionalColumn>>(ResourceBatchLogFieldsEnum.getAdvancedConditionColumns());

	}

	
	@ResponseBody
	@RequestMapping(value = "/manualCollection.json")
	public RestServiceResult<Map<String, Object>> manualCollection(HttpServletRequest request,
			@RequestParam(value = "data", defaultValue = "", required = true) String data,
			@RequestParam(value = "tableId", defaultValue = "", required = true) Integer tableId,		
			@RequestParam(value = "rootId", defaultValue = "", required = true) Integer rootId) {
		RestServiceResult<Map<String, Object>> result = new RestServiceResult<Map<String, Object>>();
		// 获取当前用户信息
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getBatchDetail", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		try {
			//获取当前使用的版本ID
			Integer contentId = errorDataService.getcontentId();
			Map<String, Object> resultMap= manualService.manualCollection(data, tableId, rootId, userInfo, contentId,absolutepath);
			
			result.setData(resultMap);
		} catch (ServiceException e) {
			// 捕获抛出的异常
			String error = e.getMsg();
			JSONObject jb = new JSONObject();
			jb.put("error", error);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg(error);
			result.setData(jb);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("ManualController#manualCollection", "各区县数据采集执行异常！ERROR:" + e.getMessage(), e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("各区县数据采集执行异常！请联系管理员处理");
			return result;
		}
		return result;
	}



}
